﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ShareMarking.Systems.Service;
using ShareMarking.Service.Headine.Services;
using ShareMarking.Service.Headine.Entities;
using System.Web.Configuration;
using Sharemarking.Web.weixin;

namespace Sharemarking.Web.tools
{
    /// <summary>
    /// headline_ajax 的摘要说明
    /// </summary>
    public class headline_ajax : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string action = DTRequest.GetQueryString("action");
            string url = DTRequest.GetQueryString("url");
            switch(action)
            {
                case "headSelect": //分享内容
                    headSelect(context);
                    break;
                case "viewsAdd":
                    viewsSelect(context);
                    break;
                case "getconfig":
                    GetConfig(context,url);
                    break;
                default:
                    break;
            }
        }

        //获取配置
        public void GetConfig(HttpContext context,string url)
        {
            long timestamp;//生成签名的时间戳
            string nonceStr;//生成签名的随机串
            string signature;//签名
            string jstick;
            string str;
            string appId = WebConfigurationManager.AppSettings["WeixinAppId"];
            string appSecret = WebConfigurationManager.AppSettings["WeixinAppSecret"];
            JSSDKHelp jssdk = new JSSDKHelp();
            jstick = jssdk.Getjstick();
            timestamp = JSSDKHelp.CreateTimestamp();
            nonceStr = JSSDKHelp.CreateNonce_str();
            signature = JSSDKHelp.GetSignature(jstick, nonceStr, timestamp,url, out str);

            context.Response.Clear(); //清除所有之前生成的Response内容
            string result = "{\"timestamp\":\"" + timestamp + "\", \"nonceStr\":\"" + nonceStr + "\",\"signature\":\"" + signature + "\"}";
            context.Response.Write(result);

            context.Response.End(); //停止Response后续写入动作，保证Response内只有我们写入内容
        }

        //分享
        private void headSelect(HttpContext context)
        {
            string xlsid = DTRequest.GetString("xlsid");//模板ID
            string name = DTRequest.GetString("name");//头条姓名
            string area = DTRequest.GetString("area");//头条地区
            var entity = new head();
            var result = headService.Instance.Get_SingleEntity_byWhere(head._.area == area && head._.name == name && head._.xlsID == xlsid);
            //判断分享的内容是否存在
            if(result != null)
            {
                //分享内容存在  增加分享次数
                result.scount += 1;
                headService.Instance.Update_Entity(result);
                context.Response.Write("{\"status\":1, \"msg\":\"分享成功\"}");
            }
            else
            {
                //分享内容不存在  添加一条数据
                entity.area = area;
                entity.name = name;
                entity.xlsID = Convert.ToInt32(xlsid);
                entity.vcount = 0;
                entity.scount = 1;
                headService.Instance.Add_Entity(entity);
                context.Response.Write("{\"status\":1, \"msg\":\"分享成功\"}");
            }
        }

        //浏览
        private void viewsSelect(HttpContext context)
        {
            string xlsid = DTRequest.GetString("xlsid");//模板ID
            string name = DTRequest.GetString("name");//头条姓名
            string area = DTRequest.GetString("area");//头条地区
            var entity = new head();
            var result = headService.Instance.Get_SingleEntity_byWhere(head._.area == area && head._.name == name && head._.xlsID == xlsid);
            //判断分享的内容是否存在
            if(result != null)
            {
                //分享内容存在  增加分享次数
                result.vcount += 1;
                headService.Instance.Update_Entity(result);
                context.Response.Write("{\"status\":1, \"msg\":\"添加成功\"}");
            }
            else
            {
                //分享内容不存在  添加一条数据
                entity.area = area;
                entity.name = name;
                entity.xlsID = Convert.ToInt32(xlsid);
                entity.vcount = 1;
                entity.scount = 0;
                headService.Instance.Add_Entity(entity);
                context.Response.Write("{\"status\":1, \"msg\":\"添加成功\"}");
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}